<template>
  <view class="invoice-issuing-page">
    <image class="page-bg" src="/static/images/bg.png" mode="aspectFill" />
    <view class="gradient-bg" :style="{ top: navBarHeight + 'px' }"></view>
    <CustomNavbar title="发票开具" />
    
    <view class="page-content">
      <!-- 开票中状态 -->
      <view class="loading-container" v-if="status === 'issuing'">
        <view class="loading-circle">
          <view class="progress-ring">
            <view class="progress-segment"></view>
          </view>
          <text class="loading-text">开票中</text>
        </view>
        <text class="loading-message">发票开具中,请稍候~</text>
      </view>
      
      <!-- 已完成状态 -->
      <view class="success-container" v-if="status === 'completed'">
        <view class="success-icon">
           <image src="/static/icons/icon-dui.png" alt=""/>
        </view>
        <text class="success-message">发票开具成功</text>
      </view>
      
      <!-- 底部按钮 -->
      <view class="button-group">
        <button 
          v-if="status === 'issuing'" 
          class="return-btn single-btn" 
          @click="handleReturn"
        >
          返回发票列表
        </button>
        <template v-if="status === 'completed'">
          <button class="return-btn" @click="handleReturn">返回发票列表</button>
          <button class="view-btn" @click="handleView">查看发票</button>
        </template>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import CustomNavbar from '@/components/CustomNavbar.vue'

const navBarHeight = ref(88)
const status = ref('issuing') // 'issuing': 开票中, 'completed': 已完成
const invoiceAmount = ref('')
let timer = null

onLoad((options) => {
  if (options.amount) {
    invoiceAmount.value = options.amount
  }
  
  // 模拟开票过程，3秒后切换到完成状态
  timer = setTimeout(() => {
    status.value = 'completed'
  }, 3000)
})

onMounted(() => {
  const windowInfo = uni.getWindowInfo()
  const statusBarHeight = windowInfo.statusBarHeight || 0
  navBarHeight.value = statusBarHeight + 44
})

onUnmounted(() => {
  if (timer) {
    clearTimeout(timer)
  }
})

const handleReturn = () => {
  // 返回发票列表页面
  uni.navigateBack({
    delta: 2
  })
}

const handleView = () => {
  // 跳转到发票预览页面
  uni.navigateTo({
    url: `/pages/invoice-preview/invoice-preview?amount=${invoiceAmount.value}&imageUrl=${invoiceImageUrl.value}`,
    success: () => {
      console.log('跳转到发票预览页面成功')
    },
    fail: (err) => {
      console.error('跳转失败:', err)
      uni.showToast({
        title: '跳转失败',
        icon: 'none',
        duration: 2000
      })
    }
  })
}
</script>

<style scoped>
.invoice-issuing-page {
   
  position: relative;
  z-index: 1;
  background: rgba(246, 246, 246, 1);
}

.page-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
  pointer-events: none;
  object-fit: cover;
}

.gradient-bg {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  pointer-events: none;
  background: rgba(255, 255, 255, 0.8);
}

.page-content {
  position: relative;
  z-index: 2;
  padding: 40rpx 32rpx;
  padding-bottom: 200rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 200rpx);
}

/* 加载状态 */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
}

.loading-circle {
  position: relative;
  width: 240rpx;
  height: 240rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 40rpx;
}

.progress-ring {
  position: absolute;
  top: 0;
  left: 0;
  width: 210rpx;
  height: 210rpx;
  border-radius: 50%;
  border: 16rpx solid rgba(240, 240, 240, 1);
  /* box-sizing: border-box; */
}

.progress-segment {
  position: absolute;
  top: 0;
  left: 0;
  width: 210rpx;
  height: 210rpx;
  border-radius: 50%;
  border: 16rpx solid transparent;
  border-top-color: rgba(78, 138, 254, 1);
  box-sizing: border-box;
  animation: rotate 3s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.loading-text {
  position: relative;
  z-index: 2;
  color: rgba(78, 138, 254, 1);
  font-family: HarmonyOS Sans SC;
  font-size: 32rpx;
  font-weight: 500;
  line-height: 38rpx;
}

.loading-message {
  color: rgba(153, 153, 153, 1);
  font-family: HarmonyOS Sans SC;
  font-size: 28rpx;
  font-weight: 400;
  line-height: 32rpx;
}

/* 成功状态 */
.success-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
}

.success-icon {
  width: 200rpx;
  height: 200rpx;
  border-radius: 50%;
  background: rgba(78, 138, 254, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 40rpx;
  box-shadow: 0px 8rpx 16rpx rgba(78, 138, 254, 0.3);
}
.success-icon image{
  width: 100%;
  height: 100%;
}

.checkmark {
  color: rgba(255, 255, 255, 1);
  font-size: 120rpx;
  font-weight: 700;
  line-height: 1;
}

.success-message {
  color: rgba(51, 51, 51, 1);
  font-family: HarmonyOS Sans SC;
  font-size: 32rpx;
  font-weight: 500;
  line-height: 38rpx;
}

/* 底部按钮 */
.button-group {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  gap: 24rpx;
  padding: 32rpx;
}

.return-btn,
.view-btn {
  flex: 1;
  height: 88rpx;
  border-radius: 44rpx;
  font-family: HarmonyOS Sans SC;
  font-size: 32rpx;
  font-weight: 500;
  line-height: 88rpx;
  text-align: center;
  border: none;
}

.return-btn.single-btn {
  width: 100%;
  background: rgba(78, 138, 254, 1);
  color: rgba(255, 255, 255, 1);
}

.return-btn:not(.single-btn) {
  background: rgba(255, 255, 255, 1);
  color: rgba(78, 138, 254, 1);
  border: 2rpx solid rgba(78, 138, 254, 1);
}

.view-btn {
  background: rgba(78, 138, 254, 1);
  color: rgba(255, 255, 255, 1);
}

.return-btn::after,
.view-btn::after {
  border: none;
}

.return-btn:active,
.view-btn:active {
  opacity: 0.8;
}
</style>

